﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;

namespace RoketPack
{
    class Certificate
    {
        private static string m_CertificateLocation = "http://www.roket-games.com/cacert.crt";
        private static string p_CertificateThumbprint = "F643F4960E00D404F109FF8A53FBED2911B96D37";
        private static string p_FormattedThumbprint = "F643F496 0E00D404 F109FF8A 53FBED29 11B96D37";

        internal static bool EnsureInstalled()
        {
            X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);

            try
            {
                store.Open(OpenFlags.ReadWrite);

                bool found = false;
                foreach (X509Certificate2 cert in store.Certificates)
                {
                    if (cert.Thumbprint == Certificate.CertificateThumbprint)
                    {
                        found = true;
                        break;
                    }
                }

                store.Close();

                if (!found)
                {
                    // Show certificate wizard.
                    byte[] data = Downloader.Grab(Certificate.m_CertificateLocation);

                    // Re-open the store in read-write mode and add the certificate.
                    try
                    {
                        store.Open(OpenFlags.ReadWrite);
                        X509Certificate2 cert = new X509Certificate2(data);
                        store.Add(cert);
                        store.Close();
                        return true;
                    }
                    catch (CryptographicException)
                    {
                        // Could not add certificate.
                        return false;
                    }
                }
                else
                    return true;
            }
            catch (CryptographicException)
            {
                // Could not detect certificate precense.
                return false;
            }
        }

        internal static bool IsInstalled()
        {
            X509Store store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);

            try
            {
                store.Open(OpenFlags.ReadOnly);

                bool found = false;
                foreach (X509Certificate2 cert in store.Certificates)
                {
                    if (cert.Thumbprint == Certificate.CertificateThumbprint)
                    {
                        found = true;
                        break;
                    }
                }

                store.Close();

                return found;
            }
            catch (CryptographicException)
            {
                // Could not detect certificate precense.
                throw;
            }
        }

        internal static string CertificateThumbprint
        {
            get
            {
                return Certificate.p_CertificateThumbprint;
            }
        }

        internal static string FormattedThumbprint
        {
            get
            {
                return Certificate.p_FormattedThumbprint;
            }
        }
    }
}
